package org.example.day12;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;

/**
 * 4.1.14 condition.awaitUninterruptibly()
 *      ①condition.await() 当线程处于等待状态时被中断，会报异常
 *      ②condition.awaitUninterruptibly() 当线程处于等待状态被中断，不会报异常
 **/
public class MyThread {
    private ReentrantLock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public void testMethod(){
        try {
            lock.lock();
            System.out.println("wait begin");
//            condition.await();
            condition.awaitUninterruptibly();
            System.out.println("wait end");
        }
//        catch (InterruptedException e) {
//            e.printStackTrace();
//            System.out.println("catch");
//        }
        finally {
            lock.unlock();
        }
    }

    public static void main(String[] args) {
        try {
            MyThread m1 = new MyThread();
            Thread1A ta = new Thread1A(m1);
            ta.start();
            Thread.sleep(3000);
            ta.interrupt(); //处于等待状态的线程被中断会报异常。
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

class Thread1A extends Thread{
    private MyThread m1;

    public Thread1A(MyThread m1) {
        this.m1 = m1;
    }

    @Override
    public void run() {
        m1.testMethod();
    }
}